package com.woniu.service.impl;

import com.woniu.dao.DeptDao;
import com.woniu.entity.Dept;
import com.woniu.entity.DeptExample;
import com.woniu.entity.PageInfo;
import com.woniu.service.DeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService {
  @Resource
  private DeptDao deptDao;


    @Override
    public List<Dept> getAllDepts() {
        return deptDao.selectByExample(null);
    }

    @Override
    public void addDept(Dept dept) {
        deptDao.insert(dept);
    }

    @Override
    public List<Dept> listByPage(PageInfo pageInfo) {
        Integer countRow = Integer.parseInt(deptDao.countByExample(null) +"");
        pageInfo.setCountRow(countRow);
        pageInfo.setCountPage(pageInfo.getCountRow()%pageInfo.getPageSize()==0?pageInfo.getCountRow()/pageInfo.getPageSize(): pageInfo.getCountRow()/ pageInfo.getPageSize()+1);
        DeptExample example = new DeptExample();
        example.setLimit(pageInfo.getPageSize());
        example.setOffset((long) ((pageInfo.getPageNum()-1)* pageInfo.getPageSize()));
        List<Dept> deptList = deptDao.selectByExample(example);

        return deptList;
    }

    @Override
    public void deleteSelect(List<Integer> deptnos) {

        DeptExample deptExample = new DeptExample();
        DeptExample.Criteria criteria =  deptExample.createCriteria();
        criteria.andDeptnoIn(deptnos);
        deptDao.deleteByExample(deptExample);

    }
}
